/**  
* Copyright (c) 2008, Noel Lynch All rights reserved. Redistribution and use  
* in source and binary forms, with or without modification, are permitted  
* provided that the following conditions are met: Redistributions of source  
* code must retain the above copyright notice, this list of conditions and the  
* following disclaimer. Redistributions in binary form must reproduce the above  
* copyright notice, this list of conditions and the following disclaimer in the  
* documentation and/or other materials provided with the distribution. Neither  
* the name of Noel Lynch nor the names of contributors may be used to  
* endorse or promote products derived from this software without specific prior  
* written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND 
* CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT  
* NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A  
* PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR  
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,  
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,  
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;  
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,  
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR  
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF  
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.  
*/
// Jul 29, 2008

package com.sceneworker.monitorpopup;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.Hashtable;

import javax.swing.JDialog;
import javax.swing.tree.TreePath;

import com.acarter.jmejtree.JMEJTreePopUp;
import com.acarter.scenemonitor.dialog.MonitorDialog;
import com.jme.image.Texture;
import com.jme.light.Light;
import com.jme.renderer.pass.BasicPassManager;
import com.jme.renderer.pass.Pass;
import com.jme.scene.Node;
import com.jme.scene.PassNode;
import com.jme.scene.PassNodeState;
import com.jme.scene.Skybox;
import com.jme.scene.Spatial;
import com.jme.scene.TriMesh;
import com.jme.scene.state.BlendState;
import com.jme.scene.state.LightState;
import com.jme.scene.state.RenderState;
import com.jme.scene.state.TextureState;
import com.jme.util.export.Savable;
import com.jmex.terrain.TerrainPage;

public class SceneWorkerPopUp extends JMEJTreePopUp {
	
	static	private		Object		mcl_buffer = null;
	
	static	private		Hashtable<Class, Class>		mcla_classToMenuBindings;
	static	private		Hashtable<Class, Class>		mcla_interfaceToMenuBindings;
	
	static	public	void	registerClassToMenu(Class cl_objectClass, Class cl_menuClass) {
		if(mcla_classToMenuBindings == null) {
			mcla_classToMenuBindings = new Hashtable<Class, Class>();
			mcla_interfaceToMenuBindings = new Hashtable<Class, Class>();
			bindMenusToClassTypes();
		}
		
		mcla_classToMenuBindings.put(cl_objectClass, cl_menuClass);
	}
	
	static	public	void	registerInterfaceToMenu(Class cl_interfaceClass, Class cl_menuClass) {
		if(mcla_classToMenuBindings == null) {
			mcla_classToMenuBindings = new Hashtable<Class, Class>();
			mcla_interfaceToMenuBindings = new Hashtable<Class, Class>();
			bindMenusToClassTypes();
		}
		
		mcla_interfaceToMenuBindings.put(cl_interfaceClass, cl_menuClass);
	}
	
	public
	SceneWorkerPopUp(JDialog cl_dlg) {
		super(cl_dlg);
		
		if(mcla_classToMenuBindings == null) {
			mcla_classToMenuBindings = new Hashtable<Class, Class>();
			mcla_interfaceToMenuBindings = new Hashtable<Class, Class>();
			bindMenusToClassTypes();
		}
		
		// get the dialog
		MonitorDialog	lcl_dlg = (MonitorDialog)getOwnerContainer();
		
		// get the selected path
		TreePath		lcl_treePath = lcl_dlg.getTree().getSelectionPath();
		
		// get it's class and it's corresponding menu class
		Class<?>		lcl_classKey = lcl_treePath.getLastPathComponent().getClass();
		ArrayList<Class>	lcla_interfacesImplemented = new ArrayList<Class>();	
		Class			lcl_menuClass;
		
		while(lcl_classKey != null) {
			lcl_menuClass = mcla_classToMenuBindings.get(lcl_classKey);
			
			if(lcl_menuClass != null) {
				createMenuFromClass(lcl_menuClass, lcl_treePath);
			}
			
			// get all interfaces implemented by the class
			Class<?>	lcla_interfaces[] = lcl_classKey.getInterfaces();
			
			for(Class<?> lcl_interface : lcla_interfaces) {
				// is this interface already added?
				if(lcla_interfacesImplemented.indexOf(lcl_interface) == -1) {
					lcla_interfacesImplemented.add(lcl_interface);
				}
			}
			
			// get the super class
			lcl_classKey = lcl_classKey.getSuperclass();
		}
		
		// now create menus for each single occurence of the interface
		for(Class lcl_interface : lcla_interfacesImplemented) {
			lcl_menuClass = mcla_interfaceToMenuBindings.get(lcl_interface);
			
			if(lcl_menuClass != null) {
				createMenuFromClass(lcl_menuClass, lcl_treePath);
			}
		}
	}
	
	static	private	void	bindMenusToClassTypes() {
		mcla_classToMenuBindings.put(BlendState.class, BlendStateMenu.class);
		mcla_classToMenuBindings.put(Pass.class, PassMenu.class);
		mcla_classToMenuBindings.put(Texture.class, TextureMenu.class);
		mcla_classToMenuBindings.put(BasicPassManager.class, PassManagerMenu.class);
		mcla_classToMenuBindings.put(TerrainPage.class, TerrainPageMenu.class);
		mcla_classToMenuBindings.put(TriMesh.class, TriMeshMenu.class);
		mcla_classToMenuBindings.put(Node.class, NodeMenu.class);
		mcla_classToMenuBindings.put(PassNode.class, PassNodeMenu.class);
		mcla_classToMenuBindings.put(Spatial.class, SpatialMenu.class);
		mcla_classToMenuBindings.put(TextureState.class, TextureStateMenu.class);
		mcla_classToMenuBindings.put(PassNodeState.class, PassNodeStateMenu.class);
		mcla_classToMenuBindings.put(LightState.class, LightStateMenu.class);
		mcla_classToMenuBindings.put(Light.class, LightMenu.class);
		mcla_classToMenuBindings.put(Skybox.class, SkyBoxMenu.class);
		mcla_classToMenuBindings.put(String.class, KeyCommandMenu.class);
		mcla_interfaceToMenuBindings.put(Savable.class, CoreMenu.class);
		
		
	}
	
	private	void	createMenuFromClass(Class cl_menuClass, TreePath cl_selectedPath) {
		try {
			Constructor<?>		lcl_menuCtor = cl_menuClass.getDeclaredConstructor(TreePath.class, SceneWorkerPopUp.class);
			lcl_menuCtor.setAccessible(true);
			Object				lcl_menu = lcl_menuCtor.newInstance(cl_selectedPath, this);
		} catch (SecurityException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalArgumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (NoSuchMethodException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InstantiationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public	Object	getObjectBuffer() {
		return mcl_buffer;
	}
	
	public	void	setObjectBuffer(Object cl_obj) {
		mcl_buffer = cl_obj;
	}
}
